import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-08
 * Time:22:49
 */

/**
 * 力扣219:存在重复元素Ⅱ
 */

public class containsNearbyDuplicate {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        //哈希表
        Map<Integer, Integer> map = new HashMap<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            int num = nums[i];
            if (map.containsKey(num) && i - map.get(num) <= k) {
                return true;
            }
            map.put(num, i);
        }
        return false;
    }
}
